package com.taru.order.dao;



import com.taru.order.entity.Note;

import com.taru.order.entity.NoteLikes;
import com.taru.order.entity.User;
import com.taru.order.util.DbUtil;
import com.taru.order.util.StringUtil;

import java.awt.*;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NoteDaoImpl  {

    UserDaoimpl userDaoimpl=new UserDaoimpl();

    /**
     * 添加小记
     * @param note
     * @return
     * @throws IOException
     */
    public int addNote(Note note){

        String sql="insert into `tb_note`(context,img_url,user_id,create_time) values (?,?,?,now())";
        int code= DbUtil.update(sql,note.getContext(),note.getImgUrl(),note.getUserId());
        DbUtil.close();
        return code;
    }

    /**
     * 修改小记
     * @param note
     * @return
     * @throws IOException
     */
    public int modNote(Note note) {

        String sql=" update `tb_note` set context=?,img_url=?,mod_time=now() where id=?";
        int code=DbUtil.update(sql,note.getContext(),note.getImgUrl(),note.getId());
        DbUtil.close();
        return code;
    }

    /**
     * 批量删除小记
     * @param list
     * @return
     * @throws Exception
     */
    public int removeNotes(List list) {
        //拼接IN
        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<list.size();i++){
            stringBuffer.append("\'");
            stringBuffer.append(list.get(i));
            stringBuffer.append("\',");
        }
        String sql="delete from `tb_note` where id in ("+stringBuffer.substring(0,stringBuffer.length()-1)+")";
        int code=DbUtil.update(sql);
        DbUtil.close();
        return code;

    }

    /**
     * 获取所有小记
     * @param pageNum
     * @param pageSize
     * @return
     * @throws IOException
     */
    public List listNotes(Integer pageNum, Integer pageSize)  {
        String sql="SELECT tb_note.*,tb_user.username FROM `tb_note` left  outer join tb_user on tb_user.user_id=tb_note.user_id  ORDER BY create_time desc LIMIT ?, ?";
        List<Map<String,Object>> list =DbUtil.query(sql,(pageNum-1)*pageSize,pageSize);
        List<Note> noteList=new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                Note note=new Note();
                note.setId(StringUtil.valueOf(map.get("id")));
                note.setCreateTime(StringUtil.valueOf(map.get("create_time")));
                note.setModTime(StringUtil.valueOf(map.get("mod_time")));
                note.setLikes(Integer.parseInt(StringUtil.valueOf(map.get("likes"))));
                note.setContext(StringUtil.valueOf(map.get("context")));
                note.setUserId(StringUtil.valueOf(map.get("user_id")));
                User user=userDaoimpl.userInfo(StringUtil.valueOf(map.get("user_id")));
                note.setUser(user);
                note.setImgUrl(StringUtil.valueOf(map.get("img_url")));
                noteList.add(note);
            }
        }
        DbUtil.close();
        return noteList;
    }





    /**
     * 获取所有小记总数
     * @return
     * @throws IOException
     */
    public Long queryToral()  {
        String sql=" select count(*) from `tb_note`";
        Long toral=DbUtil.getTotalCount(sql);
        DbUtil.close();
        return toral;
    }


    /**
     *
     * 查询点赞数
     * @param noteId
     * @return
     * @throws IOException
     */
    public Integer queryLikes(String noteId)  {
        String sql=" select likes from `tb_note` where id=?";
        List<Map<String,Object>>list=DbUtil.query(sql,noteId);
        int toral=0;
        if(list!=null && list.size()>0){
            Map<String,Object> map=list.get(0);
            toral=(Integer)map.get("likes");
        }
        DbUtil.close();
        return toral;
    }

    /**
     * 修改点赞数
     * @param
     * @return
     * @throws IOException
     */
    public int modLikes(Integer likes,String noteId)  {
        String sql=" update `tb_note` set likes=? where id=?";
        int code=DbUtil.update(sql,likes,noteId);
        DbUtil.close();
        return code;
    }
    /**
     * 添加点赞
     */
    public int insertNoteLikes(NoteLikes likes){
        String sql="insert into tb_note_like(note_id,user_id) values(?,?)";
        Integer like=queryLikes(likes.getNoteId());
        int code1=0;
        int code2=0;
        try{
            code1=modLikes(like+1,likes.getNoteId());
            code2=DbUtil.update(sql,likes.getNoteId(),likes.getUserId());
        }catch (Exception e){
            e.printStackTrace();
            DbUtil.rollback();
        }
        return code1 & code2;
    }
    /**
     * 取消点赞
     */
    public int deleteNoteLikes(NoteLikes likes){
        String sql="delete from tb_note_like where user_id=? and note_id=?";
        Integer like=queryLikes(likes.getNoteId());
        int code1=0;
        int code2=0;
        if(like<=0){
            return 0;
        }
        try{
            code1=modLikes(like-1,likes.getNoteId());
            code2=DbUtil.update(sql,likes.getNoteId(),likes.getUserId());
        }catch (Exception e){
            e.printStackTrace();
            DbUtil.rollback();
        }
        return code1 + code2;
    }

    /**
     * 判断有没有被点赞过
     */
    public NoteLikes queryNoteLikes(NoteLikes likes){
        String sql="select t.* from tb_note_like t where user_id=? and note_id=?";
        List<Map<String,Object>> list=DbUtil.query(sql,likes.getUserId(),likes.getNoteId());
        NoteLikes noteLikes=null;
        if(list !=null&&list.size()>0){
            noteLikes=new NoteLikes();
            Map<String,Object> map=list.get(0);
            noteLikes.setId(StringUtil.valueOf(map.get("id")));
            noteLikes.setNoteId(StringUtil.valueOf(map.get("note_id")));
            noteLikes.setUserId(StringUtil.valueOf(map.get("user_id")));
        }
        DbUtil.close();
        return noteLikes;
    }
    /**
     * 删除点赞记录
     */
    public void deletedLikes(NoteLikes likes){
        String sql="delete t.* from tb_note_like t where user_id=? and note_id=?";
        int code=DbUtil.update(sql,likes.getUserId(),likes.getNoteId());
        DbUtil.close();
    }

    /**
     * 获取小记详情
     * @param id
     * @return
     * @throws IOException
     */
    public Note getNoteById(String id) {
        String sql="  select * from `tb_note` where id=?";
        List<Map<String,Object>> list=DbUtil.query(sql,id);
        Note note=null;
        if(list!=null && list.size()>0){
            Map<String, Object> map=list.get(0);
            note=new Note();
            note.setId(StringUtil.valueOf(map.get("id")));
            note.setCreateTime(StringUtil.valueOf(map.get("create_time")));
            note.setModTime(StringUtil.valueOf(map.get("mod_time")));
            note.setLikes(Integer.parseInt(StringUtil.valueOf(map.get("likes"))));
            note.setContext(StringUtil.valueOf(map.get("context")));
            note.setUserId(StringUtil.valueOf(map.get("user_id")));
            note.setImgUrl(StringUtil.valueOf(map.get("img_url")));
            User user=userDaoimpl.userInfo(StringUtil.valueOf(map.get("user_id")));
            note.setUser(user);
        }
        return note;
    }
    /**
     * 根据用户id查询小记
     */
    public List<Note> getNoteByUserId(String userId){
        String sql="  select * from `tb_note` where user_id=?";
        List<Map<String,Object>> list=DbUtil.query(sql,userId);
        List<Note> noteList=new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String, Object> map:list){
                Note note=new Note();
                note.setId(StringUtil.valueOf(map.get("id")));
                note.setCreateTime(StringUtil.valueOf(map.get("create_time")));
                note.setModTime(StringUtil.valueOf(map.get("mod_time")));
                note.setLikes(Integer.parseInt(StringUtil.valueOf(map.get("likes"))));
                note.setContext(StringUtil.valueOf(map.get("context")));
                note.setUserId(StringUtil.valueOf(map.get("user_id")));
                note.setImgUrl(StringUtil.valueOf(map.get("img_url")));
                User user=userDaoimpl.userInfo(StringUtil.valueOf(map.get("user_id")));
                note.setUser(user);
                noteList.add(note);
            }
        }
        return noteList;
    }
    /**
     * 按照时间查询小记列表
     */
    public List<Note> getNoteByYearMonth(String year,String month){
        String sql=" SELECT * FROM tb_note WHERE YEAR(create_time) = ? and MONTH(create_time)=? ORDER BY create_time desc";
        List<Map<String,Object>> list=DbUtil.query(sql,year,month);
        List<Note> noteList=new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String, Object> map:list){
                Note note=new Note();
                note.setId(StringUtil.valueOf(map.get("id")));
                note.setCreateTime(StringUtil.valueOf(map.get("create_time")));
                note.setModTime(StringUtil.valueOf(map.get("mod_time")));
                note.setLikes(Integer.parseInt(StringUtil.valueOf(map.get("likes"))));
                note.setContext(StringUtil.valueOf(map.get("context")));
                note.setUserId(StringUtil.valueOf(map.get("user_id")));
                note.setImgUrl(StringUtil.valueOf(map.get("img_url")));
                User user=userDaoimpl.userInfo(StringUtil.valueOf(map.get("user_id")));
                note.setUser(user);
                noteList.add(note);
            }
        }
        return noteList;
    }
}
